package com.yellowpage.wf.job;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {

	public void send(String username , String password , String smtp_server , String to_mail_address , String description , String title) {
		Authenticator auth = null;
		String from_mail_address = null;
		Properties mailProps = null;
		Session mailSession = null;
		MimeMessage message = null;
		MimeMultipart multi = null;
		BodyPart textBodyPart = null;
		try {
			//String username = "bblu@guanghua.sh.cn";
			//String password = "12345678";
			//String smtp_server = "master.guanghua.sh.cn";
			//String from_mail_address = username;
			//String to_mail_address = "bblu@guanghua.sh.cn";
			from_mail_address = username;
			auth = new PopupAuthenticator(username, password);

			mailProps = new Properties();
			mailProps.put("mail.smtp.auth", "true");

			mailProps.put("username", username);

			mailProps.put("password", password);

			mailProps.put("mail.smtp.host", smtp_server);
			//Session mailSession = Session.getDefaultInstance(mailProps, auth);
			mailSession = Session.getInstance(mailProps, auth);
			message = new MimeMessage(mailSession);
			message.setFrom(new InternetAddress(from_mail_address));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(
					to_mail_address));
			message.setSubject(title);
			
			multi = new MimeMultipart();
			textBodyPart = new MimeBodyPart();
			textBodyPart.setContent(description, "text/html;charset=UTF-8");
			//textBodyPart.setText("");
			multi.addBodyPart(textBodyPart);
			message.setContent(multi);
			message.saveChanges();
			Transport.send(message);
		} catch (Exception ex) {
			System.err.println("邮件发送失败的原因是：" + ex.getMessage());
			System.err.println("具体错误原因：");
			ex.printStackTrace(System.err);
		} finally {
			auth = null;
			from_mail_address = null;
			mailProps = null;
			mailSession = null;
			message = null;
			multi = null;
			textBodyPart = null;
		}
	}

	public static void main(String[] args) {
		SendMail s = new SendMail();
		//String username = "bblu@guanghua.sh.cn";
		//String password = "12345678";
		//String smtp_server = "master.guanghua.sh.cn";
		//String from_mail_address = username;
		//String to_mail_address = "bblu@guanghua.sh.cn";
		s.send("bblu@guanghua.sh.cn", "12345678", "master.guanghua.sh.cn", "bblu@guanghua.sh.cn" , "<a href=http://localhost:8080/portal>http://localhost:8080/portal</a>" , "邮件测试");
	}

}

class PopupAuthenticator extends Authenticator {
	private String username;
	private String password;

	public PopupAuthenticator(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(this.username, this.password);
	}
}
